/**
 * 
 */
package com.antilia.infomaresme.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;

import com.antilia.infomaresme.data.dao.interfaces.ICategoriesDao;
import com.antilia.infomaresme.data.dao.interfaces.IItemsDao;
import com.antilia.infomaresme.data.dataobjects.Category;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.pages.details.DetailsLinkPanel;
import com.antilia.infomaresme.pages.details.DetailsPanel;
import com.antilia.infomaresme.provider.ItemsDataProvider;
import com.antilia.infomaresme.resources.InfoModalWindow;
import com.google.inject.Inject;

/**
 * @author Ernesto
 *
 */
public class ChooseCategoryPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	@Inject
	private ICategoriesDao categoriesDao;
	
	@Inject
	private IItemsDao iItemsDao;
	
	private Category selected;
	
	private Category place;
	
	private ItemsDataProvider  dataProvider;
	
	private AjaxFallbackDefaultDataTable<Item> items;
	
	private ModalWindow modal;
	/**
	 * @param id
	 */
	public ChooseCategoryPanel(String id) {
		super(id);
		setOutputMarkupId(true);		
		modal = new InfoModalWindow("modal");
		modal.setTitle("Detalles");
		modal.setInitialHeight(500);
		modal.setInitialWidth(700);
		modal.setResizable(false);
		add(modal);
	}
	
	@Override
	protected void onBeforeRender() {
		RepeatingView choosenCategory = new RepeatingView("choosenCategory");
		addOrReplace(choosenCategory);
		
		WebMarkupContainer wm  = new WebMarkupContainer(choosenCategory.newChildId());
			choosenCategory.add(wm);
			wm.add(new TopLevelLinkPanel("category",Model.of("Todas")) {
				
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					selected = null;
					target.addComponent(ChooseCategoryPanel.this);	
				}
			}
		);
			
		if(selected != null) {
			List<Category> parents = selected.getParentChain();
			for(Category category: parents) {
				wm  = new WebMarkupContainer(choosenCategory.newChildId());
				choosenCategory.add(wm);
				wm.add(new CategoryLinkPanel("category", category) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						selected = getCategory();
						target.addComponent(ChooseCategoryPanel.this);
					}
				});
			}
			
			wm  = new WebMarkupContainer(choosenCategory.newChildId());
			choosenCategory.add(wm);
			wm.add(new CategoryLinkPanel("category", selected) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						selected = getCategory();
						target.addComponent(ChooseCategoryPanel.this);
					}
				});
		} 
		
		
		RepeatingView choosenPlace = new RepeatingView("choosenPlace");
		addOrReplace(choosenPlace);
		
		wm  = new WebMarkupContainer(choosenPlace.newChildId());
			choosenPlace.add(wm);
			wm.add(new TopLevelLinkPanel("category",Model.of("Todos")) {
				
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					place = null;
					target.addComponent(ChooseCategoryPanel.this);	
				}
			}
		);
			
		if(place != null) {
			List<Category> parents = place.getParentChain();
			for(Category category: parents) {
				wm  = new WebMarkupContainer(choosenPlace.newChildId());
				choosenPlace.add(wm);
				wm.add(new CategoryLinkPanel("category", category) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						place = getCategory();
						target.addComponent(ChooseCategoryPanel.this);
					}
				});
			}
			
			wm  = new WebMarkupContainer(choosenPlace.newChildId());
			choosenPlace.add(wm);
			wm.add(new CategoryLinkPanel("category", place) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						place = getCategory();
						target.addComponent(ChooseCategoryPanel.this);
					}
				});
		} 
		
		
		CategoriesListPanel categorias = new CategoriesListPanel("categorias", selected, Model.of("Categorias:")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				selected = getSelected();
				target.addComponent(ChooseCategoryPanel.this);
			}
			
			@Override
			protected List<Category> findTopLevel() {
				return categoriesDao.findTopLevel();
			}
		};
		
		addOrReplace(categorias);
				
		CategoriesListPanel places = new CategoriesListPanel("places", place, Model.of("Lugares:")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				place = getSelected();
				target.addComponent(ChooseCategoryPanel.this);
			}
			
			@Override
			protected List<Category> findTopLevel() {
				return categoriesDao.findTopLevelGeographic();
			}
		};
		
		addOrReplace(places);
		
		List<IColumn<Item>> columns = new ArrayList<IColumn<Item>>();	
		columns.add(new PropertyColumn<Item>(new Model<String>("Detalles"), null , "price") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void populateItem(
					org.apache.wicket.markup.repeater.Item<ICellPopulator<Item>> item,
					String componentId, final IModel<Item> rowModel) {
				// TODO Auto-generated method stub
				item.add(new DetailsLinkPanel(componentId, rowModel) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						modal.setContent(new DetailsPanel(modal.getContentId(),rowModel));
						modal.show(target);
					}
				});
			}
		});
		columns.add(new PropertyColumn<Item>(new Model<String>("Descripcion"), null, "title"));
		//columns.add(new PropertyColumn<Item>(new Model<String>("Description"), null , "description"));		
		columns.add(new PropertyColumn<Item>(new ResourceModel("location"), null, "geographicLocation"));
		columns.add(new PropertyColumn<Item>(new Model<String>("Price"), "price" , "price"));
		
		
		
 		dataProvider = new ItemsDataProvider(iItemsDao) {
 		 	
 		 	private static final long serialVersionUID = 1L;

			@Override
 		 	public Category getCategory() {
 		 		return selected;
 		 	}
 		 	
 		 	@Override
 		 	public Category getPlace() {
 		 		return place;
 		 	}
 		};		
		items  = new AjaxFallbackDefaultDataTable<Item>("items", columns, dataProvider, 10);
		addOrReplace(items);
		super.onBeforeRender();
	}

	public ICategoriesDao getCategoriesDao() {
		return categoriesDao;
	}

	public void setCategoriesDao(ICategoriesDao categoriesDao) {
		this.categoriesDao = categoriesDao;
	}

	public Category getSelected() {
		return selected;
	}

	public void setSelected(Category selected) {
		this.selected = selected;
	}

	public IItemsDao getiItemsDao() {
		return iItemsDao;
	}

	public void setiItemsDao(IItemsDao iItemsDao) {
		this.iItemsDao = iItemsDao;
	}

}
